// 04jyhs/shoppingMallAdmin/publishGoods/publishGoods.js
var co = require('../../../resource/js/common.js')
var app = getApp()
var appData = app.Data
var _this, _data, isShow
Page({
   data: {
      cateList: [],
      cateArr: [],
      pick_i: -1,
      goodsInfo: '',
      // 添加照片显示
      isImage: false,
      imgSrc: [],
      src: [],
      img: '',
      // 传递的商品
      productList: '',
      isPicker: false,
      idx: '',
      // 显示库存
      isCount: 1,
      isMoreSku: true,
      MoreSku: '',
      // 暂存switch值
      tempSkuCount: '',
   },
   // 进入页面加载
   onLoad: function(opt) {
      _this = this
      _data = this.data
      if (!co.empty(opt.obj)) {
         app.Data.pick_i = opt.idx
         var obj = JSON.parse(opt.obj)
         console.log("obj",obj)
         _this.setData({
            MoreSku: obj.sku_list,
            productList: obj,
            isImage: true,
            isPicker: true,
            idx: opt.idx,
            src: obj.Products_JSON.ImgPath
         })
         // 存入全局规格数据
         app.Data.skus = obj.sku_list
         if (_data.MoreSku[0].Products_Count == -1) {
            this.setData({ isCount: 0 })
         }
      } else {
         app.Data.skus = []
         _this.setData({ pick_i: app.Data.pick_i })
      }
      _this.getSort()
   },
   // 获取返回的多规格刷新
   onShow: function() {
      var skus = app.Data.skus
      if (!co.empty(skus)) {
         if (skus.length == 1) {
            if (skus[0].Products_Count == -1) {
               isShow = false
            } else {
               isShow = true
            }
         }
         _this.setData({
            isMoreSku: false,
            MoreSku: skus,
         })
      } else {
         _this.setData({
            isMoreSku: true,
         })
      }
      console.log("【输出多规格】====", _data.MoreSku)
   },
   // 获取自定义商品分类
   getSort: function() {
      co.getMealApi('jyhs/Prdmg/getBizCategoryList', {
         Biz_ID: appData.shopInfo.Biz_ID
      }, function(res) {
         console.log("【获取商家自定义分类】===", res)
         var cate = res.data
         var c = []
         if (res.code == 0) {
            for (var k in cate) {
               c.push(cate[k].Category_Name)
            }
            _this.setData({
               cateArr: c,
               cateList: cate
            })
         }
      })
      console.log("【获取商家自定义分类】===", _data.cateList, _data.cateArr)
   },
   // 分类选择器
   pickerClick: function(e) {
      // console.log("【分类选择器】===", e.detail.value)
      _this.setData({
         pick_i: e.detail.value,
         isPicker: false
      })
      app.Data.pick_i = e.detail.value
   },
   // 选择图片
   chooseImage: function() {
      wx.chooseImage({
         count: 1, // 最多可以选择的图片张数，默认9
         sizeType: ['compressed'], // original 原图，compressed 压缩图，默认二者都有
         sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
         success: function(res) {
            _this.setData({
               isImage: true
            })
            // 图片上传
            wx.uploadFile({
               url: 'https://api.jyhs.ls293.cn/api/Upload/img', //图片上传接口
               filePath: res.tempFilePaths[0],
               name: 'image',
               formData: {
                  dir: 'product/img' + app.Data.shopInfo.Biz_ID
               },
               success: function(res) {
                  console.log("res====", res)
                  var s = JSON.parse(res.data)
                  console.log("【res====】", s.data.wwwPath)
                  var ImgList = _data.imgSrc.concat(s.data.wwwPath)
                  var srcList = _data.src.concat(s.data.wwwPath)
                  _this.setData({
                     imgSrc: ImgList,
                     src: srcList,
                     img: s.data.relativePath
                  })
               }
            })
         }
      })
   },
   // 删除照片
   deleteImage: function(e) {
      var index = e.currentTarget.id
      var littleSrc = _data.src.splice(index,1)
      littleSrc = JSON.stringify(littleSrc[0])
      console.log(littleSrc)
      littleSrc = littleSrc.replace("http://h5.jyhs.ls293.cn", "")
      littleSrc = littleSrc.replace('"',"")
      littleSrc = littleSrc.replace('"', "")
      _this.setData({
         src: _data.src
      })
      co.getJyhsApi('api/Upload/deleteImg', {
         imageFile: littleSrc
      }, function(res) {
         console.log("【获取返回】===", res)
      })
   },
   // 提交商品信息
   submitGoodsInfo: function(e) {
      var param = e.detail.value
      console.log("prama======", param)
      param.Biz_ID = appData.shopInfo.Biz_ID
      // 判断switch是否选择
      if (_data.tempSkuCount != '') {
         param.Products_Count = _data.tempSkuCount
      }
      // 将规格封装成数组
      if (_data.MoreSku == '' || _data.MoreSku.length == 1) {
         var obj = [{
            Sku_Name: param.Products_Name,
            Products_PriceX: param.Products_PriceX,
            //LunchBox_Price: param.LunchBox_Price,
            Products_Count: param.Products_Count
         }]
         this.setData({
            MoreSku: obj
         })
         //delete param.LunchBox_Price
         delete param.Products_Count
         delete param.Products_PriceX
      }
      // 修改或者增加
      if (co.empty(_data.idx)) {
         if (_data.pick_i == -1) {
            wx.showModal({
               title: '温馨提示',
               content: '请选择产品分类',
            })
            return
         }
         param.Products_JSON = JSON.stringify({ ImgPath: _data.imgSrc})
         param.Products_BizCategory = _data.cateList[_data.pick_i].Category_ID
      } else {
         // 默认不选择或者选择新的分类
         if (_data.pick_i == -1) {
            param.Products_BizCategory = _data.productList.Products_BizCategory
         } else {
            param.Products_BizCategory = _data.cateList[_data.pick_i].Category_ID
         }
         var img
         if (_data.isImage == true) {
            console.log("_data.src", _data.src)
            console.log("_data.imgSrc", _data.imgSrc)
            if (!co.empty(_data.imgSrc)) {
               img = _data.src
            } else {
               img = _data.src
            }
         } else {
            img = ""
         }
         param.Products_ID = _data.productList.Products_ID
         param.Products_JSON = JSON.stringify({ ImgPath: img })
      }
      param.skujosn = JSON.stringify(_data.MoreSku)
      console.log("【获取param】===", param)
      co.getMealApi('jyhs/Prdmg/saveProduct', param, function(res) {
         if (res.code == 0) {
            console.log("【获取添加的商品数据】===", res)
            app.Data.goodsList = res.data
            wx.navigateTo({
               url: '/04jyhs/shoppingMallAdmin/publishGoods/GoodsDetail/GoodsDetail',
            })
         } else {
            co.modal(res.msg)
         }
      })
   },
   // 库存点击
   stockShow: function(e) {
      var id = e.currentTarget.id
      var isCount = _data.isCount
      var tempSkuCount = ''
      if (isCount == 1) {
         isCount = 0
         tempSkuCount = -1
      } else {
         isCount = 1
         tempSkuCount = ''
      }
      this.setData({
         isCount: isCount,
         tempSkuCount: tempSkuCount
      })
   },
   //管理商品规格
   manageSku: function() {
      wx.navigateTo({
         url: '/04jyhs/shoppingMallAdmin/publishGoods/AddStandard/AddStandard'
      })
   },
})